package server.business.shop.handler;

import server.ScriptEnum;
import server.ServerConfigCache;
import server.ServerEngin;
import server.ServerHandler;
import server.business.login.ILoginScript;
import server.business.login.LoginScript;
import server.business.shop.config.ShopConfig;
import server.commonData.User;
import server.msgBean.ShopMessage.ShopBuyItemReq;

import com.core.initialize.Message;
import com.google.protobuf.GeneratedMessage;

/**
 * 商城购买物品
 * @author King
 *
 */
public class BuyItemHandler extends ServerHandler
{

	@Override
	public Class<? extends GeneratedMessage> initBodyClass()
	{
		return ShopBuyItemReq.class;
	}

	@Override
	protected void handler(Message msg, User user) throws Throwable
	{
		ShopBuyItemReq req = msg.getBody();
		ShopConfig config = ServerConfigCache.shopMap.get(req.getItemId());
		if(config==null)
			return;
		//判断元宝是否够
		if(user.getYuanbao()<config.needYuanbao)
		{
			System.err.println("元宝不够");
			return;
		}
		//扣除元宝
		user.addDelYuanbao(-config.needYuanbao);
		//获得vip
		user.addDelVipEndTime(config.awardVipTime);
		//获得金币
		user.addMoney(config.awardMoney);
		//刷新信息给客户端
		ILoginScript scripte = ServerEngin.getInstance().getScriptM().getScript(ScriptEnum.LOGIN_SCRIPT);
		scripte.refrechVipMoneyGoldInfo(user);
	}
}
